package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import com.example.myapplication.R;
import java.util.ArrayList;
import java.util.List;

public class InterestSelectionActivity extends AppCompatActivity {
    private CheckBox cbSports, cbReading, cbMusic, cbTravel, cbGames;
    private Button btnConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest_selection);

        // 1. 初始化所有控件
        cbSports = findViewById(R.id.cb_sports);
        cbReading = findViewById(R.id.cb_reading);
        cbMusic = findViewById(R.id.cb_music);
        cbTravel = findViewById(R.id.cb_travel);
        cbGames = findViewById(R.id.cb_games);
        btnConfirm = findViewById(R.id.btn_confirm);

        // 2. 设置文字样式（加粗特定部分）
        setGameTextStyle();
        setTravelTextStyle();
        setMusicTextStyle();
        setReadTextStyle();
        setSportTextStyle();

        // 3. 确认按钮点击事件（添加未选择提示）
        btnConfirm.setOnClickListener(v -> {
            List<String> selectedInterests = new ArrayList<>();
            if (cbSports.isChecked()) selectedInterests.add("FPS类多人射击对战游戏");
            if (cbReading.isChecked()) selectedInterests.add("开放世界生存建造类游戏");
            if (cbMusic.isChecked()) selectedInterests.add("叙事剧情驱动型RPG游戏");
            if (cbTravel.isChecked()) selectedInterests.add("多人在线战术竞技类MOBA游戏");
            if (cbGames.isChecked()) selectedInterests.add("肉鸽动作冒险类卡牌游戏");

            // 新增：未选择任何兴趣时提示
            if (selectedInterests.isEmpty()) {
                Toast.makeText(this, "至少选择一个兴趣哦~", Toast.LENGTH_SHORT).show();
                return; // 不关闭页面，让用户继续选择
            }

            String result = String.join(",", selectedInterests);
            Intent intent = new Intent();
            intent.putExtra("selected_interest", result);
            setResult(RESULT_OK, intent);
            finish();
        });
    }

    // 以下样式设置保持不变，但优化了代码格式
    private void setGameTextStyle() {
        String text = "肉鸽动作冒险类卡牌游戏";
        SpannableString spannable = new SpannableString(text);
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        spannable.setSpan(boldSpan, 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        cbGames.setText(spannable);
    }

    private void setTravelTextStyle() {
        String text = "多人在线战术竞技类MOBA游戏";
        SpannableString spannable = new SpannableString(text);
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        spannable.setSpan(boldSpan, 9, 13, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        cbTravel.setText(spannable);
    }

    private void setReadTextStyle() {
        String text = "叙事剧情驱动型RPG游戏";
        SpannableString spannable = new SpannableString(text);
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        spannable.setSpan(boldSpan, 7, 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        cbReading.setText(spannable);
    }

    private void setMusicTextStyle() {
        String text = "开放世界生存建造类游戏";
        SpannableString spannable = new SpannableString(text);
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        spannable.setSpan(boldSpan, 0, 4, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        cbMusic.setText(spannable);
    }

    private void setSportTextStyle() {
        String text = "FPS类多人射击对战游戏";
        SpannableString spannable = new SpannableString(text);
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        spannable.setSpan(boldSpan, 0, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        cbSports.setText(spannable);
    }
}